package com.yao.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yao.context.BaseContext;
import com.yao.exception.custom.PathExcption;
import com.yao.mapper.ArticleMapper;
import com.yao.pojo.Article;
import com.yao.pojo.ArticleQuery;
import com.yao.pojo.ArticleQueryVO;
import com.yao.pojo.PageResult;
import com.yao.service.ArticleService;
import com.yao.utils.AliOSSUtils;
import com.yao.utils.PathCorrect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.LocalTime;

@Slf4j
@Service
public class ArticleServiceImpl implements ArticleService {

    private ArticleMapper articleMapper;
    private AliOSSUtils aliOSSUtils;

    @Autowired
    public void ArticleServiceImpl(ArticleMapper articleMapper, AliOSSUtils aliOSSUtils){
        this.articleMapper = articleMapper;
        this.aliOSSUtils = aliOSSUtils;

    }

    /**
     * 发布文章
     * @param article
     */
    @Override
    public void addArticle(Article article) {
        article.setCreateUser(BaseContext.getCurrentId());
        article.setCreateTime(LocalDateTime.now());
        article.setUpdateTime(LocalDateTime.now());

        articleMapper.addArticle(article);
    }

    /**
     * 修改文章
     * @param article
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean changeArticle(Article article) {
        Article articleContext = articleMapper.findEqualArticle(article.getId());
        if (articleContext.getCreateUser() != BaseContext.getCurrentId()) return false;

        article.setUpdateTime(LocalDateTime.now());
        articleMapper.changeArticle(article);
        return true;
    }

    /**
     * 根据文章ID删除文章
     * @param id
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean deleteArticle(Integer id) {
        Article equalArticle = articleMapper.findEqualArticle(id);

        Integer createUser = equalArticle.getCreateUser();
        Integer currentId = BaseContext.getCurrentId();
        if (createUser != currentId){
            log.info("当前用户ID...{},操作用户ID...{}的文章",currentId,createUser);
            return false;
        }

        String url = equalArticle.getCoverImg();
        url = PathCorrect.correct(url);
        aliOSSUtils.deleteFile(url);
        articleMapper.deleteArticle(id);

        return true;
    }

    @Override
    public PageResult conditionQuery(ArticleQueryVO articleQueryVO) {
        PageHelper.startPage(articleQueryVO.getPageNum(), articleQueryVO.getPageSize());
        ArticleQuery articleQuery = BeanUtil.copyProperties(articleQueryVO, ArticleQuery.class);

        LocalDateTime begin = LocalDateTime.of(articleQueryVO.getBegin(), LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(articleQueryVO.getBegin(), LocalTime.MAX);
        articleQuery.setBegin(begin);
        articleQuery.setEnd(end);

        Page<Article> articles = (Page<Article>) articleMapper.selectArticle(articleQuery);

        return new PageResult(articles.getTotal() , articles.getResult());
    }
}
